package cn.keniykey.mc.viewlibrary.toolkit;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;

public final class VerAnalysisUtil {
	private VerAnalysisUtil() {}
	
	private static  Pattern pattern = Pattern.compile("\\d\\.\\d+(\\.\\d{1,2})?");
	private static int[] number_version = converVersion(Bukkit.getServer().getVersion());
	
	private static int[] converVersion(String version) {
		final Matcher matcher = pattern.matcher(version);
		if(matcher.find()) {
			final String[] array_str = matcher.group().split("\\.");
			final int[] array_int = new int[array_str.length];
			for(int index = 0; index < array_int.length; index++) array_int[index] = Integer.valueOf(array_str[index]);
			return array_int;
		}
		throw new RuntimeException("Unable to convert version number correctly(无法正确转化版本号)");
	}
	/**
	 * 当前运行版本对于指定版本的对比结果：
	 * 0：无变化 -1：旧版 1：新版
	 */
	public static byte comparison(String version) {
		final int[] array_int = converVersion(version);
		for(int index = 0; index < array_int.length && index < number_version.length; index++) {
			final int checkint = array_int[index];
			final int nowint = number_version[index];
			if(nowint > checkint) return 1;
			if(checkint > nowint) return -1;
		}
		return 0;
	}
	/**
	 * 当前运行版本对于指定版本来说是新版或现版
	 */
	public static boolean isAfter(String version) {
		return (comparison(version) >= 0);
	}
	/**
	 * 当前运行版本对于指定版本来说是旧版
	 */
	public static boolean isBefore(String version) {
		return (comparison(version) < 0);
	}
	/**
	 * 当前运行版本对于指定版本来说是现版
	 */
	public static boolean isMatch(String version) {
		return (comparison(version) == 0);
	}
}